#!/usr/bin/python3

# SPDX-FileCopyrightText: Red Hat, Inc.
# SPDX-License-Identifier: GPL-2.0-or-later
from __future__ import absolute_import
from __future__ import print_function

import sys
import argparse

parser = argparse.ArgumentParser()

parser.add_argument('-ic', dest='inputConnection',
                    help='Set input libvirt connectio url')
parser.add_argument('-o', dest='output',
                    help='Set output method')
parser.add_argument('-of', dest='outputFormat',
                    help='Set output image format')
parser.add_argument('-oa', dest='outputAllocation',
                    help='Set output allocation format')
parser.add_argument('--vdsm-image-uuid', dest='vdsmImageId',
                    help='Vdsm image UUID', action='append')
parser.add_argument('--vdsm-vol-uuid', dest='vdsmVolId',
                    help='Vdsm volume UUID', action='append')
parser.add_argument('--vdsm-vm-uuid', dest='vdsmVmId',
                    help='VM UUID')
parser.add_argument('--password-file', dest='passwordFile',
                    help='Read connection password from a file')
parser.add_argument('--vdsm-ovf-output', dest='vdsmOvfOutput',
                    help='Output directory for ovf output')
parser.add_argument('-os', dest='outputStorage',
                    help='Output directory for the images')
parser.add_argument('--machine-readable', dest='machineReadable',
                    action='store_true',
                    help='Set the terminal output to be readable')
parser.add_argument('-v', dest='verbose',
                    action='store_true',
                    help='Enable verbose messages for debugging.')
parser.add_argument('-x', dest='libguestfsTrace',
                    action='store_true',
                    help='Enable tracing of libguestfs API calls.')
parser.add_argument('vmname', nargs='?')

options = parser.parse_args()
if options.vmname is None:
    if not options.machineReadable:
        sys.stderr.write(
                "Either VM name or --machine-readable must be specified\n")
        parser.print_usage()
        sys.exit(1)

    # Output some capabilities and quit
    print("virt-v2v")
    print("libguestfs-rewrite")
    print("vdsm-compat-option")
    print("input:libvirt")
    print("input:ova")
    print("output:rhev")
    print("output:vdsm")
    print("convert:enterprise-linux")
    print("convert:windows")
    sys.exit(0)

elapsed_time = 0


def write_output(msg):
    sys.stdout.write(msg)
    sys.stdout.flush()

def write_trace(msg):
    sys.stderr.write(msg)
    sys.stderr.flush()

def write_progress():
    global elapsed_time
    for i in range(101):
        write_output('    (%s/100%%)\r' % str(i))
        elapsed_time = elapsed_time + 1

write_output('[   %d.0] Opening the source -i libvirt\n' % elapsed_time)
elapsed_time = elapsed_time + 1
write_output('[   %d.0] Creating an overlay to protect\n' % elapsed_time)
elapsed_time = elapsed_time + 1

if options.libguestfsTrace:
    write_trace("libguestfs: trace: internal_autosync = 0\n")
    write_trace("libguestfs: sending SIGTERM to process 12345\n")
    write_trace("libguestfs: trace: shutdown = 0\n")
    write_trace("libguestfs: trace: close\n")
    write_trace("libguestfs: closing guestfs handle 0x1e265f0 (state 0)\n")

for i, o in enumerate(options.vdsmImageId):
    write_output('[  %d.0] Copying disk %d/2 to %s/%s/images/%s\n' %
                 (elapsed_time, i+1, options.outputStorage,
                  options.vdsmVmId, o))

    # Immitate some verbose messages
    # NOTE: Most verbose messages go to stderr, but some go to stdout. This can
    # potentialy mess with our parsing routine.
    if options.verbose:
        write_output("target_file = %s\n" % options.vdsmVolId[i])
        write_output("target_format = raw\n")
        write_output("target_estimated_size = 123456789\n")
        write_output("target_overlay = /var/tmp/v2vovl344e53.qcow2\n")

    write_progress()
    write_output('[ %d.0] Creating output metadata\n' % elapsed_time)
    write_output('[ %d.0] Finishing off\n' % elapsed_time)
